<?php
/**
 * Klasse zum Aufbau und Validierung von Formularen
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_Form_Formular_Handler
{
	private $arrRow			= array();

	private $formId			= NULL;
	private $method			= NULL;
	private $action			= NULL;
	private $enctype		= NULL;


	/**
	 * @param string $_method	: post|get
	 * @param string $_action	: aufzurufende Seite
	 * @param string $_enctype	: multipart/form-data
	 * */
	function __construct ($_formId = 'yapary_formular', $_method =  '', $_action = '', $_enctype = 'multipart/form-data') {
		$this->formId	= $_formId;
		$this->method	= $_method;
		$this->action	= $_action;
		$this->enctype	= $_enctype;
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param int $i	: Auszugebende Zeile
	 * @param string $hidden	: Wenn Hidden-Text angegeben wird, wird das Formular geschlossen angezeigt und per Klick auf Text geoeffnet
	 * @return Yapary_Form_Formular_Row
	 * */
	public function addRow ($desc, $_hiddenText = NULL) {
		return $this->arrRow[] = new Yapary_Form_Formular_Row($desc, $_hiddenText);
	}


	/**
	 * Liefert eine Zeile zurueck.
	 * @param int $i	: Auszugebende Zeile
	 * @return Yapary_Form_Formular_Row
	 * */
	public function get ($i) {
		if (!isset($this->arrRow[$i])) {
			return NULL;
		}

		return $this->arrRow[$i];
	}


	/**
	 * @desc Liefert alle Zeilen zurueck.
	 * @return Yapary_Form_Formular_Row[]
	 * */
	public function getList () {
		return $this->arrRow;
	}


	/**
	 * Loescht einen Eintrag.
	 * @param string $id	: Zu loeschender Eintrag
	 * @return bool
	 * */
	public function delete ($i) {
		if (!isset($this->arrRow[$i])) {
			return false;
		}

		unset($this->arrRow[$i]);
		return true;
	}


	/**
	 * @desc Gibt an, ob das Formular bereits versendet wurde.
	 * @return mixed
	 * */
	private function getValue ($key) {
		switch ($this->method) {
			case 'post':
				if (isset($_POST[$key])) {
					return $_POST[$key];
				}
				break;
			case 'get':
				if (isset($_GET[$key])) {
					return $_GET[$key];
				}
				break;
		}

		return NULL;
	}


	/**
	 * @desc Gibt an, ob das Formular bereits versendet wurde.
	 * @return bool
	 * */
	public function hasSend () {
		$valid = true;

		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				if ($oRow->hasSend() === false) {
					$valid = false;
				}
			}
		}

		return $valid;
	}


	/**
	 * @desc Gibt an, ob das Formular bereits versendet wurde.
	 * @return bool
	 * */
	public function hasError () {
		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				if ($oRow->hasError() === true) {
					return true;
				}
			}
		}

		return false;
	}


	/**
	 * @desc Liefert alle entstandenen Fehler.
	 * @return array
	 * */
// 	public function getError () {
// 		$arrError = array();

// 		if (count($this->arrRow)) {
// 			/* @var $oRow Yapary_Form_Formular_Row */
// 			foreach ($this->arrRow as $key => $oRow) {
// 				$arrError[] = $oRow->getError();
// 			}
// 		}

// 		return $arrError;
// 	}


	/**
	 * @desc Liefert die Anzahl an internen und externen Fehlern.
	 * @return int
	 * */
	public function getErrorCount () {
		$count = 0;

		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				if ($oRow->hasError()) {
					$count += $oRow->getErrorCount();
				}
			}
		}

		return $count;
	}


	/**
	 * @desc Gibt an, dass Fehlercodes aus der Validierung angezeigt werden sollen
	 * @return void
	 * */
	public function enableErrorReporting ($_enable = true) {
		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				$oRow->enableErrorReporting($_enable);
			}
		}
	}


	/**
	 * @desc Gibt an, dass Beschreibugnstexte angezeigt werden sollen
	 * @return void
	 * */
	public function enableDesc ($_enable = true) {
		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				$oRow->enableDesc($_enable);
			}
		}
	}


	/**
	 * @desc Gibt an, dass Beschreibugnstexte angezeigt werden sollen
	 * @return void
	 * */
	public function enableRowColor ($_enable = true) {
		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				$oRow->enableRowColor($_enable);
			}
		}
	}


	/**
	 * @desc Fuegt dem Element elName einen externen Fehler hinzu.
	 * @return string $elName	: Elementname
	 * @return string $_error	: Fehlermeldung
	 * */
	public function setExternalError ($elName, $_error) {
		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				if ($oRow->setExternalError($elName, $_error) === true) {
					return true;
				}
			}
		}

		return false;
	}


	/**
	 * @desc Validiert alle Formulareingaben.
	 * @return int|NULL	: Validierung erfolgreich=true, sonst false
	 * */
	public function validate () {
		$valid = true;

		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				if ($oRow->validate() === false) {
					$valid = false;
				}
			}
		}

		return $valid;
	}


	/**
	 * @desc Gibt das Formular aus.
	 * @return string
	 * */
	public function display() {
		$form = '';
		if (count($this->arrRow)) {
			/* @var $oRow Yapary_Form_Formular_Row */
			foreach ($this->arrRow as $key => $oRow) {
				$form .=	$oRow->display();
			}
		}

		return '<form class="form-horizontal" id="'.$this->formId.'" method="'.$this->method.'" action="'.$this->action.'" enctype="'.$this->enctype.'" class="Yapary_Form_Formular">
					'.$form.'
				</form>';
	}


}


?>